#!/bin/bash

echo "=========================================="
echo "检查系统依赖"
echo "=========================================="
echo ""

# 1. 检查系统基础库（应该都存在）
echo "1. 系统基础库："
for lib in libc.so.6 libm.so.6 libpthread.so libstdc++.so.6; do
    if ldconfig -p 2>/dev/null | grep -q "$lib"; then
        echo "   ✓ $lib"
    else
        echo "   ✗ $lib (缺失)"
    fi
done

# 2. 检查X11库
echo ""
echo "2. X11图形库："
x11_ok=true
for lib in libX11.so.6 libxcb.so.1; do
    if ldconfig -p 2>/dev/null | grep -q "$lib"; then
        echo "   ✓ $lib"
    else
        echo "   ✗ $lib (缺失)"
        x11_ok=false
    fi
done

# 3. 检查OpenGL库
echo ""
echo "3. OpenGL库："
opengl_ok=true
for lib in libGL.so.1 libGLX.so.0; do
    if ldconfig -p 2>/dev/null | grep -q "$lib"; then
        echo "   ✓ $lib"
    else
        echo "   ✗ $lib (缺失)"
        opengl_ok=false
    fi
done

# 4. 检查系统服务
echo ""
echo "4. 系统服务："
if systemctl is-active --quiet NetworkManager 2>/dev/null; then
    echo "   ✓ NetworkManager (运行中)"
elif systemctl list-unit-files | grep -q NetworkManager; then
    echo "   ⚠ NetworkManager (已安装但未运行)"
else
    echo "   ✗ NetworkManager (未安装)"
fi

if systemctl is-active --quiet polkit 2>/dev/null; then
    echo "   ✓ polkit (运行中)"
else
    echo "   ⚠ polkit (未运行，但通常不影响)"
fi

if systemctl is-active --quiet dbus 2>/dev/null; then
    echo "   ✓ dbus (运行中)"
else
    echo "   ✗ dbus (未运行)"
fi

# 5. 检查已安装的程序
echo ""
echo "5. 已安装的程序："
if [ -f /opt/netmanager/network_manager_gui ]; then
    echo "   ✓ network_manager_gui (已安装)"
    
    # 检查依赖
    missing=$(ldd /opt/netmanager/network_manager_gui 2>&1 | grep -i "not found" || true)
    if [ -z "$missing" ]; then
        echo "   ✓ 所有依赖库已找到"
    else
        echo "   ✗ 发现缺失的依赖库："
        echo "$missing" | sed 's/^/     /'
    fi
else
    echo "   ✗ network_manager_gui (未安装)"
fi

echo ""
echo "=========================================="
if [ "$x11_ok" = true ] && [ "$opengl_ok" = true ]; then
    echo "✓ 系统依赖检查通过，程序应该可以正常运行！"
else
    echo "⚠ 部分依赖缺失，可能需要安装："
    if [ "$x11_ok" = false ]; then
        echo "   sudo yum install libX11 libxcb"
    fi
    if [ "$opengl_ok" = false ]; then
        echo "   sudo yum install mesa-libGL mesa-libGLX"
    fi
fi
echo "=========================================="
